Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __repr__ method to metric objects, make them debug friendly. #481

Merged
merged 2 commits into from
Oct 30, 2019

Conversation

asherf
Copy link
Contributor

@asherf asherf commented Oct 26, 2019

before:
Screenshot 2019-10-25 18 44 10

after:
Screenshot 2019-10-25 18 43 40

Signed-off-by: Asher Foa <asher@asherfoa.com>
@@ -80,6 +80,10 @@ def collect(self):
metric.add_sample(self._name + suffix, labels, value)
return [metric]

def __repr__(self):
metric_type = type(self)
return "{0}.{1}:{2}".format(metric_type.__module__, metric_type.__name__, self._name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A repr() should in principle allow instantiation of the object. This would be a __str__. Also _type covers you more here.

Copy link
Contributor Author

@asherf asherf Oct 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as using the _type, I added an __str__ implementation that uses it.
I think having the full type path is useful in repr output which what is used in debuggers.
I updated the __repr__ implementation to use parenthesis instead of a column to be more compatible with the ability to pass eval
However, I feel that making it a 100% compatible with that (i.e. an eval(repr(metric)) that produces an identical object) will result in a string that will be too noisy and thus less useful when debugging.

Signed-off-by: Asher Foa <asher@asherfoa.com>
@brian-brazil brian-brazil merged commit 4b2d052 into prometheus:master Oct 30, 2019
@brian-brazil
Copy link
Contributor

Thanks!

@asherf asherf deleted the repr branch October 30, 2019 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants